import Cart from '../Cart'
import {connect} from 'react-redux'
import { checkout } from '../store/action/index'

const getCartProduct = state =>{
   return  state.cart.items.map(i => {
      const pro = state.product.all.find(p =>p.id === i.id);
      return {
        id: pro.id,
        title: pro.title,
        price: pro.price,
        num: i.num
      }
    })
};
const getTotalPrice = state=>{
  return  getCartProduct(state).reduce((total,cur)=>{
    return total += cur.price*cur.num;
  },0)
}

const mapStateToProps = (state) => {
  return {
    cartProducts: getCartProduct(state),
    totalPrice: getTotalPrice(state),
    checkoutStatus: state.cart.checkStatus
  }
}

// const mapDispatchToProps = dispatch=>{
//   return {
//   }
// }

const mapDispatchToProps = {
  checkout
}

//Cart->UI组件(无业务 ,逻辑)
//ContainerCart->容器组件(包含业务状态)
const ContainerCart = connect(
  mapStateToProps,
  mapDispatchToProps
)(Cart)

export default ContainerCart
